<?php
require_once 'phpQuery-onefile.php';
require_once 'MySqlHelper.php';
//phpQuery::$debug = true;

header('Content-Type: text/html; charset=gb2312');
/*
$opts = array(
		'http' => array(
				'method'=>"GET",
				'header'=>"Content-Type: text/html; charset=gb2312"
		)
);
$context = stream_context_create($opts);
echo file_get_contents('http://www.shhbm.com/news/2/');

echo file_get_contents("http://www.shhbm.com/news/2/9/2013/0311/86669.html");
*/

//$parent = phpQuery::newDocumentHtml('http://www.shhbm.com/news/2/','gb2312');
//$parent = phpQuery::newDocumentFile('http://www.shhbm.com/news/2/','text/html;charset=gb2312');
$file=file_get_contents('http://www.shhbm.com/news/2/');
$parent = phpQuery::newDocument($file);
$salesList = pq(".itembox",$parent);
//echo ($salesList->html());
$infoList = array();
foreach($salesList as $sales){
	//$title_link
	//$title_href
	//$title
		$first_line = pq($sales,$parent)->find('.box1 ');
		
		$shangchang = pq($first_line,$parent)->find('.note_1 a');
		$title = pq($first_line,$parent)->find('a:eq(1)');
		$title_link = pq($title,$parent)->attr('href');
		$sencod_line = pq($sales,$parent)->find('.box2');
		$sencod_line->find("script")->remove();
		
		//echo strip_tags($shangchang.$title."[".$title_link."]".$sencod_line);
		$mysqlHelper = MySqlHelper::getInstance();
		$sql_select = "select * from act_link where url='%s'";
		$sql_select = sprintf($sql_select,$title_link);
		$query = $mysqlHelper->query($sql_select);
		$rownum = $mysqlHelper->num_rows($query);
		if($rownum<1){
			$file1=file_get_contents($title_link);
			
			$detail = phpQuery::newDocument($file1);
			//$detail = phpQuery::newDocumentFile($title_link,'text/html;charset=gb2312');
			$all = pq('.con',$detail);
			$all->find('.jj1')->remove();
			$all->find('a')->remove();
			$all->find('.pb')->remove();
			$all->find("p[style='text-align: center']")->remove();
			
			//echo $all->html();
			$plist = $all->find('p');
			$allMsg = '';
			foreach($plist as $p){
				//print_r($p);
				$temp = strip_tags(pq($p)->html());
				if(strlen($temp)>0){
					$allMsg = $allMsg."<br>".$temp;
				}
				//strip_tags
			}
			
			$date = date('Y-m-d H:i:s');
			$sql = "insert into act_detail(title,origi_msg,create_time) values('%s','%s','%s')";
			//test iconv next time;
			$sql = sprintf($sql,mb_convert_encoding(addslashes(strip_tags($title)),"utf-8"),mb_convert_encoding(addslashes($allMsg),"utf-8"),$date);
			echo $sql;
			$query = $mysqlHelper->query($sql);
			echo $query;
			$insert_id = $mysqlHelper->insert_id();
			
			$sql="insert into act_link(detail_id,url,title,abstract,createtime) values(%d,'%s','%s','%s','%s')";
			
			$sql = sprintf($sql,$insert_id,mb_convert_encoding(addslashes(strip_tags($title)),"utf-8"),$title_link,mb_convert_encoding(addslashes(strip_tags($sencod_line)),"utf-8"),$date);
			echo $sql;
			$query = $mysqlHelper->query($sql);
		}
		//addslashes
}

?>